package com.etime.shycourse.controller.coupons;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.dto.CouponTypesDto;
import com.etime.shycourse.pojo.Coupon;
import com.etime.shycourse.services.coupons.interfaces.CouponServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 *
 * @author wb
 * @Api(tags = "后台优惠券管理数据接口")
 * @since 2022-03-15
 */
@Api(tags = "后台-优惠券管理-优惠券数据接口")
@RestController
@RequestMapping("/sys/coupon")
public class CouponController {
    @Autowired
    private CouponServices service;

    @ApiOperation("创建优惠券")
    @PostMapping("/insert")
    public BaseMessage insertCoupon(Coupon coupon, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        String token = request.getHeader ("token");
        int rtn = service.insertCoupon (coupon, token);
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("创建优惠券成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("创建优惠券失败");
        }
        return msg;
    }

    @ApiOperation("查询所有的优惠劵 分頁")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pagenum", value = "第几页"),
            @ApiImplicitParam(name = "pagesize", value = "每页多少行"),
            // 复用
            @ApiImplicitParam(name = "coupontype", value = "可根据优惠券类型进行查询"),
            @ApiImplicitParam(name = "couponstate", value = "可根据优惠券状态进行查询"),
    })
    @GetMapping("/page")
    public PageMessage<CouponTypesDto> selectCouponPage(Coupon coupon, Long pagenum, Long pagesize) {
        PageMessage<CouponTypesDto> msg = new PageMessage<> ();
        Page<CouponTypesDto> data = service.selectCouponPage (coupon, pagenum, pagesize);
        if (data != null) {
            msg.setCode (1);
            msg.setPagedata (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("据优惠劵id进行修改。返回int 成功 =1 失败=0 ")
    @PostMapping("/upCoupon")
    public BaseMessage updateCouponById(Coupon coupon, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage();
        String token = request.getHeader("token");
        int i = service.updateCouponById(coupon, token);
        if (i > 0) {
            msg.setCode(i);
            msg.setMsg("修改成功");
        } else {
            msg.setCode(0);
            msg.setMsg("修改失敗");
        }
        return msg;
    }

    @ApiOperation("据优惠劵id进行优惠劵的发布、关闭")
    @PostMapping("/upCoupons")
    public BaseMessage upCouponByIds(@RequestBody Coupon coupon, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        int i = service.updateCouponByIds (coupon);
        if (i > 0) {
            msg.setCode (i);
            msg.setMsg ("修改成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("修改失敗");
        }
        return msg;
    }

    @ApiOperation("据优惠劵id进行添加优惠劵数量. 返回int 成功 =1 失败=0 ")
    @PostMapping("/upCoupon/add")
    public BaseMessage updateCouponAddNumberById(@RequestBody Coupon coupon) {
        BaseMessage msg = new BaseMessage();
        int i = service.updateCouponAddNumberById(coupon);
        if (i > 0) {
            msg.setCode(i);
            msg.setMsg("修改成功");
        } else {
            msg.setCode(0);
            msg.setMsg("修改失敗");
        }
        return msg;
    }

    @ApiOperation("根据ID 查询优惠券")
    @GetMapping("/get/{CouponID}")
    public PojoMessage<Coupon> selectCouponById(@PathVariable Integer CouponID) {
        PojoMessage<Coupon> msg = new PojoMessage<> ();
        Coupon data = service.selectCouponById (CouponID);
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有优惠券信息");
        }
        return msg;
    }

    /**
     * 设置活动关联的优惠券
     * 查
     * 条件：
     * 1、Couponstate = 2 创建发布状态
     * 2、优惠券的发放数量<总数量 或者总数量=0
     * 3、优惠券结束使用时间>活动结束时间
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "activeend", value = "活动结束时间"),
    })
    @ApiOperation("询所有的优惠劵 用活动管理的优惠劵展示")
    @GetMapping("/list/{activeend}")
    public ListMessage<Coupon> selectCouponList(@PathVariable Date activeend) {
        ListMessage<Coupon> msg = new ListMessage<> ();
        List<Coupon> data = service.selectCouponList (activeend);
        if (data.size () > 0) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有优惠券信息");
        }
        return msg;
    }
}

